package com.foreveross.dao.imp;

import java.util.List;

import org.springframework.stereotype.Component;

import com.foreveross.common.Constants;
import com.foreveross.common.SQLUtil;
import com.foreveross.dao.HrFavorableDAO;
import com.foreveross.vo.HrFavorable;

@Component("hrFavorableDAO")
public class HrFavorableDAOImp extends SQLUtil implements HrFavorableDAO {

	@Override
	public long getFavorableListCount(String weixinID,String userId,String name,String houseId,String businessId) throws Exception {
		StringBuffer strbuBuffer = new StringBuffer();
		strbuBuffer.append(" select count(*) from HrFavorable model where 1=1 ");
		if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and model.id in(select favorable from FavorableHouse where house in ");
			strbuBuffer.append("(select b.hrHouses from HouseUser b where b.authenticationStatus = '"+Constants.AUTHENTICATIONSTATUS_SUCCESS+"'");
			strbuBuffer.append(" and b.wechatId= '"+weixinID+"'))");
			//strbuBuffer.append(" and model.isPublish = 1)");
//			strbuBuffer.append(" and model.id in(select favorable from FavorableHouse where house in ");
//			strbuBuffer.append("(select b.hrHouses from HouseUser b where wechatId= '"+weixinID+"'))");
		}
		if(name!=null && !"".equals(name)){
			strbuBuffer.append(" and  model.name like '%"+name+"%'");
		}
		if(houseId!=null && !"".equals(houseId)){
			//strbuBuffer.append(" and  model.houseId = '"+houseId+"'");
			strbuBuffer.append(" and model in (select favorable from FavorableHouse favorableHouse where favorableHouse.house ='" + houseId + "')");
			
		}
		if(businessId!=null && !"".equals(businessId)){
			strbuBuffer.append(" and  model.businessId = '"+businessId+"'");
		}
		if(userId!=null && !"".equals(userId)){
//			strbuBuffer.append(" and  model.createId = '"+userId+"'");
			strbuBuffer.append(" and  model in(select fh.favorable from FavorableHouse fh where fh.house"
					+ " in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser.id='"
					+ userId + "')))");
//			this.addAuthorityStr(strbuBuffer, userId,  "houseId");
		}
		strbuBuffer.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		return Long.parseLong(findObejctByHql(strbuBuffer.toString()).toString());
	}

	@Override
	public List<HrFavorable> getFavorableList(String weixinID, String userId,String name,String houseId,String businessId,final int pageIndex,
			final int pageSize) throws Exception {
		StringBuffer strbuBuffer = new StringBuffer();
		strbuBuffer.append(" select model from HrFavorable model where 1=1 ");
		//微信用户可以全部看到
		if(!"".equals(weixinID) && null != weixinID){
			strbuBuffer.append(" and model.id in(select favorable from FavorableHouse where house in ");
			strbuBuffer.append("(select b.hrHouses from HouseUser b where b.authenticationStatus = '"+Constants.AUTHENTICATIONSTATUS_SUCCESS+"'");
			strbuBuffer.append(" and b.wechatId= '"+weixinID+"'))");
			//strbuBuffer.append(" and model.isPublish = 1)");
//			strbuBuffer.append(" and model.id in(select favorable from FavorableHouse where house in ");
//			strbuBuffer.append("(select b.hrHouses from HouseUser b where wechatId= '"+weixinID+"'))");
		}
		if(name!=null && !"".equals(name)){
			strbuBuffer.append(" and  model.name like '%"+name+"%'");
		}
		if(houseId!=null && !"".equals(houseId)){
			//strbuBuffer.append(" and  model.houseId = '"+houseId+"'");
			strbuBuffer.append(" and model in (select favorable from FavorableHouse favorableHouse where favorableHouse.house ='" + houseId + "')");
		}
		if(businessId!=null && !"".equals(businessId)){
			strbuBuffer.append(" and  model.businessId = '"+businessId+"'");
		}
		if(userId!=null && !"".equals(userId)){
			strbuBuffer.append(" and  model in(select fh.favorable from FavorableHouse fh where fh.house"
					+ " in (select rh.hrHouses from SysRoleHouse rh where rh.sysRole in (select ur.sysRole from  SysUserRole ur where ur.sysUser.id='"
					+ userId + "')))");
//			this.addAuthorityStr(strbuBuffer, userId,  "houseId");
//			strbuBuffer.append(" and  model.createId = '"+userId+"'");
		}
		strbuBuffer.append(" or  model.isAll ='"+ Constants.IS_ALL + "'");
		strbuBuffer.append(" order by createTime desc");
		return (List<HrFavorable>) findObjectAll(strbuBuffer.toString(), pageIndex,pageSize);
	}


}
